package com.huangj.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.huangj.entity.TravelEntity;
import com.huangj.pro2_h2pop.R;
import com.huangj.pro2_h2pop.TravelPicsActivity;
import com.ms.square.android.expandabletextview.ExpandableTextView;

import java.util.ArrayList;
import java.util.List;


public class TravelLvAdapter extends BaseAdapter {

    private Context context;
    private List<TravelEntity> datas;

    public TravelLvAdapter(Context context) {
        this.context = context;
        this.datas = new ArrayList<>();
    }
    public void setDatas(List<TravelEntity> datas){
        this.datas = datas;
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView != null){
            viewHolder = (ViewHolder) convertView.getTag();
        }else {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_travelv,null);
            viewHolder.iv_head = (ImageView) convertView.findViewById(R.id.iv_head);
            viewHolder.iv_cener = (ImageView) convertView.findViewById(R.id.iv_center);
            viewHolder.tv_author = (TextView) convertView.findViewById(R.id.tv_author);
            viewHolder.tv_recommed = (TextView) convertView.findViewById(R.id.tv_recommed);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
//            viewHolder.tv_description = (TextView) convertView.findViewById(R.id.tv_description);
            viewHolder.horizontalScrollView = (HorizontalScrollView) convertView.findViewById(R.id.hsv);
            viewHolder.sc_ll = (LinearLayout) convertView.findViewById(R.id.sc_ll);
            //空间设置点击收起展开
             viewHolder.expandableTextView = (ExpandableTextView) convertView.findViewById(R.id.expand_text_view);


            convertView.setTag(viewHolder);
        }
       //头像；
        Glide.with(context).load(datas.get(position).getActivity().getUser().getPhoto_url())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .into(viewHolder.iv_head);
        //中间的大图片；
        Glide.with(context).load(datas.get(position).getActivity().getContents().get(0).getPhoto_url())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .thumbnail(0.1f)
                .crossFade()
                .into(viewHolder.iv_cener);
        //作者及推荐；
        viewHolder.tv_author.setText(datas.get(position).getActivity().getUser().getName());
        viewHolder.tv_recommed.setText(datas.get(position).getUser().getName());
        //下面的标题及简介;
        viewHolder.tv_title.setText(datas.get(position).getActivity().getTopic());
//        viewHolder.tv_description.setText(datas.get(position).getActivity().getDescription());
        // 设置显示内容
        viewHolder.expandableTextView.setText(datas.get(position).getActivity().getDescription());
        //横向移动图片；
        viewHolder.sc_ll.removeAllViews();//移除所有存在布局；
        //图片集合；
       final ArrayList<TravelEntity.ActivityBean.ContentsBean> pics = (ArrayList<TravelEntity.ActivityBean.ContentsBean>) datas.get(position).getActivity().getContents();

        for (int i = 1; i < pics.size(); i++) {
            final  int index = i;
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(220,120);
            layoutParams.leftMargin = 6;
            ImageView imageView = new ImageView(context);
            Glide.with(context).load(pics.get(i).getPhoto_url()).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TravelPicsActivity.class);
                    intent.putExtra("pics",pics);
                    intent.putExtra("index",index);
                    context.startActivity(intent);
                }
            });
            viewHolder.sc_ll.addView(imageView,layoutParams);
        }

        viewHolder.iv_cener.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TravelPicsActivity.class);
                int index = 0;
                intent.putExtra("index",index);
                intent.putExtra("pics",pics);
                context.startActivity(intent);
            }
        });
        return convertView;
    }
    class ViewHolder{
        ImageView iv_head,iv_cener;
        HorizontalScrollView horizontalScrollView;
        LinearLayout sc_ll;
        TextView tv_author,tv_recommed,tv_title;
        ExpandableTextView expandableTextView;
    }
}
